<?php
namespace Admin\Controller;
use Think\Controller;
class ContentController extends Controller{

	public function index(){
		$cid=I('cid');
		$mid=M('category')->where("cid=$cid")->getField('mid');
		$table=M('model')->where("mid=$mid")->getField('m_table_name');
		$data=M("{$table}")->where("cid=$cid")->select();
		$this->assign("content",$data);
		$this->display();
	}
	public function selectCate(){
		$categoryData=S('c');
		$data=$this->find_son($categoryData,0);
    	$this->assign('cate',$data);

		$this->display();
	}

	//添加内容
	public function add(){
		// p($_POST);exit;
		$Model=D('content');
		if(IS_POST){
			if($Model->addContent()){
				$this->success('提交成功');
			}else{
				$this->error($Model->error);
			}
		}else{
			
			//模板文件
			$arr=glob('./Template/ducen/*.html');
			$tpl=array();
			foreach ($arr as $k => $v) {
				$tpl[]=basename($v);
			}
			$html=$Model->contentShow();
			$this->assign('content',$html);
			$this->assign('tpl',$tpl);
			$this->display('add');
		}
	}
	//编辑
	public function edit(){
		$model=D('EditContent');
		if(IS_POST){
			// p($_POST);exit;
			if($model->edit()){

				$this->success('编辑成功');

			}else{
				$this->error('编辑失败');
			}
		}else{
			$contid=I('contid');
			$data=$model->contentShow();
			$this->assign('content',$data);
			$this->display();
		}
	}

	//显示所有内容
	public function allContent(){
		$table=M('model')->field('m_table_name')->select();
		dump($table);
		


	}
	//删除
	public function del(){
		$model=D('Content');
		if($model->drop()){
			echo 1;exit;
		}else{
			echo 0;exit;
		}
	}
	//编辑器的图片上传处理
	public function upload(){
		//获取存储目录
	    if ( isset( $_GET[ 'fetch' ] ) ) {
		// dump($_GET);exit;
	        header( 'Content-Type: text/javascript' );
	        echo 'updateSavePath('. json_encode(array('uploads')) .');';
	        exit;
	    }
		    $upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->savePath  =      ''; // 设置附件上传目录
		    // 上传单个文件 
		    $info   =   $upload->upload();
		    foreach($info as $v){
		    	$path=__ROOT__;
	            echo "{'url':'".$path.'/'.$info[0]['path']. "','title':'" . htmlspecialchars($_POST['pictitle'], ENT_QUOTES) . "','original':'" . $v['name'] . "','state':'SUCCESS'}";
				die();
		    }
   			
	}
	//查找子孙栏目

	public function find_son($arr,$id,$level=0){
		static $array=array();
		foreach($arr as $v){
			if($v['pid']==$id){
			$v['level']=$level+4;
			$array[]=$v;
			//到临界条件
			$this->find_son($arr,$v['cid'],$v['level']);
			}
		}
		return $array;
	}
	

}